package com.sky.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.enumeration.OperationType;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;


    //添加套餐未实现添加套餐里的菜品（insertSetmealDish实现添加套餐菜品）
    @Transactional
    @Override
    public void insertSetmealService(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);

        //添加套餐菜品
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        setmealMapper.insertSetmealDish(setmealDishes);

    }


    //查询页面分页查询模糊查询和分类表categoryId
    @Override
    public PageResult selectSetmealService(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmeals = setmealMapper.selectSetmeal(setmealPageQueryDTO);

        Page<Setmeal> page = (Page<Setmeal>) setmeals;


        return new PageResult(page.getTotal(),page.getResult());
    }


    //    删除套餐一个删除多个删除
    @Transactional
    @Override
    public void deleteSetmealDishService(Integer[] ids) {
        for (Integer id : ids) {
            Integer i = setmealMapper.selectSetmealStatus(id);
            if (i==null || i==1){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        try {
            setmealMapper.deleteSetmeal(ids);
            setmealMapper.deleteSetmealDish(ids);
        } catch (Exception e) {
            log.info("删除套餐或菜品错误");
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
        }
    }

    //查询该行套餐和套餐菜品
    @Override
    public SetmealVO selectSetmealandSetmealDish(Integer id) {

        Setmeal setmeal = setmealMapper.selectSetmealId(id);
        List<SetmealDish> setmealDishes = setmealMapper.seletSetmealDishId(id);
        SetmealVO setmealVO=new SetmealVO();

        BeanUtil.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    //修改套餐
    @Override
    public void updateSetmealService(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();


        BeanUtil.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);



        Integer[] id = {Math.toIntExact(setmealDTO.getId())};
        setmealMapper.deleteSetmealDish(id);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.stream().forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));

        setmealMapper.insertSetmealDish(setmealDishes);

    }



    //修改套餐售卖状态
    @Transactional
    @Override
    public void updateSetmealServiceStatus(Integer status, Integer id) {
        //查询菜品是否为起售状态，是可起售套餐，否则不可以
        List<Integer> integers = setmealMapper.selectDishStatusUpdateSetmealStatus(id);
        if (integers==null || integers.size()<=0){
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        for (Integer i : integers) {
            if (i==0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }


        setmealMapper.updateSetmealStatus(status,id);

    }
}
